<template>
    <div class="loginBox flexbox spacebetween">
        <section class="home-wrapper">
            <div class="login-body">
                <span class="sign-icon"></span>
                <h4 class="login-title text-center">登录</h4>
                <div class="box">
                    <article class="box-aw">
                        <el-form ref="form" :rules="formrules" :model="form">
                            <el-form-item prop="username">
                                <el-input v-model="form.username" placeholder="手机/邮箱"></el-input>
                            </el-form-item>
                            <el-form-item prop="u_password">
                                <el-input type="password" v-model="form.u_password" placeholder="请输入密码"></el-input>
                            </el-form-item>
                            <div class="form-item clearfix">
                                <el-checkbox v-model="checked" class="fl">记住密码</el-checkbox>
                                <router-link to="/forgetpawy" class="forgetpaw maincolor fr">忘记密码？</router-link>
                            </div>
                            <el-button type="primary" class="login-btn" @click="loginFun('form')">登录</el-button>
                        </el-form>
                    </article>
                    <div class="box-fr">
                        <p>没有账号？</p>
                        <router-link to="/register" class="goregister maincolor">立即注册</router-link>
                    </div>
                </div>
            </div>
        </section>
    </div>
</template>
<script>
    import '../../assets/css/home.css';
    export default {
        data(){
            return{
                form: {
                    username: '',
                    u_password: ''
                },
                formrules: {
                    username: [
                        { required: true, message: '请输入手机/邮箱', trigger: 'blur' }
                    ],
                    u_password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                },
                checked: true,  //记住密码
            }
        },
        methods: {
            //登录
            loginFun(formName){
                var _this = this;

                _this.$refs[formName].validate((valid) => {
                    if (valid) {

                        this.$httpPost('/api/index/login/login',{
                            'username': _this.form.username,
                            'u_password': _this.form.u_password,
                        }).then(response => {
                            
                            if(response.code == 200){
                                
                                window.localStorage.uid = response.msg.u_id;  //用户id
                                window.localStorage.utype = response.msg.u_type;  //账号类型   1个人   2企业
                                _this.$router.push({'path': '/userindex'})

                            }else{
                                this.$message({
                                    message: response.msg,
                                    type: 'warning'
                                });
                            }

                        })

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
                
            },
        },
        mounted(){
            
        },
    }
</script>
<style scoped>
    /* .loginBox{
        height: 100vh;
    } */
    .home-wrapper{
        margin: 0 auto;
    }
</style>

